package ru.cannabis.command;

import org.springframework.beans.factory.annotation.Autowired;
import ru.cannabis.Launcher;
import ru.cannabis.command.base.AbstractCommand;
import ru.cannabis.command.params.JSONSessionIdParams;
import ru.cannabis.command.response.JSONAuthResponse;
import ru.cannabis.game.player.Character;
import ru.cannabis.services.AuthService;

public class AuthCommand extends AbstractCommand<JSONSessionIdParams, JSONAuthResponse>
{
    /*@Autowired
    private AuthService authService;   */

	@Override
	public void innerRun(JSONAuthResponse response)
    {
        response.setResult(Launcher.getAuthService().authorize(getParams().getVid(), getSession()));

		if (response.isResult())
        {
			Character character = ru.cannabis.Launcher.getAuthService().getBySession(getSession());

            if (character != null)
            {
				//Game game = ru.cannabis.Launcher.getGameService().getPlayerGame(player);
				response.setPlayer(character);

                /*if (ru.cannabis.game != null)
                {
					if (ru.cannabis.game.getStatus() == Game.Status.WAITING || ru.cannabis.game.getStatus() == Game.Status.WAIT_FOR_START) {
						ru.cannabis.Launcher.getGameService().deletePlayerFromGame(ru.cannabis.game, player, true);
					} else {
						response.setGame(ru.cannabis.game.getId());
					}
				}*/
			}
            else
            {
				response.setResult(false);
				response.setError("Пользователь не авторизован");
			}
		}
	}

	@Override
	protected boolean checkAccess() {
		return true; // authorization is not needed for ru.cannabis.auth ru.cannabis.command
	}

}
